package com.qc.searchBug.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qc.searchBug.pojo.UserInfo;
import com.qc.searchBug.services.UserInfoServices;
import com.qc.searchBug.util.MD5;
import com.qc.searchBug.util.MyCon;

@Controller
@RequestMapping(value="/userInfo")
public class UserInfoController {
	/**
	 * 用户登录
	 */ 
	@Autowired
	private UserInfoServices userInfoBiz;
	@RequestMapping(value="/findUserLogin")
	public String findUserLogin(@RequestParam String phone,@RequestParam String pwd,HttpSession session ){
		UserInfo userinfo=userInfoBiz.findUserLogin(phone, pwd);
		if(null!=userinfo){
			session.setAttribute(MyCon.USER_INFO,userinfo);
			return "index";
		}
		return "index";
	}
	/**
	 * 用户退出
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/exitSys")
	public String exitSys(HttpSession session){
		session.removeAttribute("userInfo");
		return "index";
		
	}
	/**
	 * 注册
	 */
	@RequestMapping(value="/userRegister")
	public String userRegister(UserInfo userinfo){
		Map<String,Object> map=new HashMap<String,Object>(); 
		map.put("phone", userinfo.getPhone());
		map.put("pwd",MD5.getMD5(userinfo.getPassword())); //
		map.put("email",userinfo.getMailbox());
		map.put("nickname",userinfo.getNickname());
		userInfoBiz.userRegisterServices(map);
		return "redirect:/userInfo/exitSys";
	}
	/**
	 * 手机验证修改密码
	 */
	@ResponseBody
	@RequestMapping(value="/updatePasswordByPhoneCode")
	public Object updatePasswordByPhoneCode(@RequestParam String password,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(MyCon.USER_INFO);
		int userId=userInfo.getUserid();
		int i =userInfoBiz.updatePwdByPhoneCode(password,userId);
		return i+"";
		
	}
}